package com.dx.mes.service.spc.service;


import com.dx.common.table.ppt.*;
import com.dx.mes.common.dao.*;
import com.dx.mes.common.table.rtnBean.SimpleRtnBeen;
import com.dx.mes.common.transcoder.util.*;
import com.dx.mes.service.spc.tx.Xpcmsdt.XpcmsrdtI;
import com.dx.mes.service.spc.tx.Xpcmsdt.XpcmsrdtIA;
import com.dx.mes.service.spc.tx.Xpcmsdt.XpcmsrdtIA2;
import com.dx.mes.service.spc.tx.Xpcmsdt.XpcmsrdtO;
import com.dx.mes.service.spc.tx.Xpspcchk.XpspcchkI;
import com.dx.mes.service.spc.tx.Xpspcchk.XpspcchkIA;
import com.dx.mes.service.spc.tx.Xpspcchk.XpspcchkO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import static com.dx.mes.common.bean.def.GenericDef.*;


@Service("fqpedcdataService")
public class FqprepdataService implements IFqprepdataService {
	private LogUtils logUtils;

	@Autowired
	IRetPrdInfoDao retPrdInfoDao;
	@Autowired
	IBisMItemDao bisMItemDao;
	@Autowired
	private IBisPamDao bisPamDao;
	@Autowired
	ISpcRetMfgDataDao spcRetMfgDataDao;
	@Autowired
	IRetWODao retWoDao;
	@Autowired
	private IXpspcchkService xpspcchkService;

	public String subMainProc(String evt_no,String strInMsg) {
		AppContext.clear();
		AppContext.setCurrServiceName(FqprepdataService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FqprepdataService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");
		
		String strOutMsg = "";
		XpcmsrdtI inTrx = new XpcmsrdtI();
		XpcmsrdtO outTrx= new XpcmsrdtO();
		outTrx.setTrx_id("FQPEDCDAT");
		outTrx.setType_id("O");
		try {
			inTrx = JacksonUtil.fromJson(strInMsg, XpcmsrdtI.class);
			SubMainProc2(inTrx, outTrx);
			if (!outTrx.getRtn_code().equals(RETURN_CODE_OK)) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}else{
				outTrx.setRtn_code("0000000");
				outTrx.setRtn_mesg("success");
				spcRetMfgDataDao.flushCurSession();
			}
		} catch (Exception ex) {
			outTrx.setRtn_code("0000001");
			outTrx.setRtn_mesg(ex.getMessage());
			logUtils.error(ex.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			strOutMsg = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + strOutMsg + "]");
		}
		return strOutMsg;

	}

	public void SubMainProc2(XpcmsrdtI inTrx, XpcmsrdtO outTrx) {
		String lRc;
		String gszMsgBuf = "";
		String prdSeqId = inTrx.getPrd_seq_id();
		//机台生成的CSV文件只存EDC，不送SPC
		//rep_unit_fk 上报单元 单片上报没有设置这个属性，批量上报时默认为T（就是机台的意思？）
		/*if ("T".equals(inTrx.getRep_unit_fk())) {
			lRc = reportEqptSPCDatqa(inTrx);
			outTrx.setRtn_code(lRc);
			return ;
		}*/

		String flag = inTrx.getFlag();

	/*	Ret_prd_info retPrdInfo = (Ret_prd_info) retPrdInfoDao.get(prdSeqId);
		if (retPrdInfo == null) {
			gszMsgBuf = "not Found Record IN Ret_prd_info";
			outTrx.setRtn_mesg(gszMsgBuf);
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			return ;
		}*/

		//自动上报初始化intrx falg ='A'
		//inTrx = initIary(inTrx,flag);
		//TODO: 自动上报需要check设备在站点有无维护吗？
		/*SimpleRtnBeen rtnBeen = setEDCDataValue(retPrdInfo,inTrx);
		if (rtnBeen.getlRc() != 0000000) {
			outTrx.setRtn_code(rtnBeen.getlRc()+"");
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return ;
		}*/

		/*Ret_wo retWo = retWoDao.get(retPrdInfo.getWo_id_fk());
		if (retWo == null) {
			gszMsgBuf = "根据玻璃找不到工单信息[" + retPrdInfo.getWo_id_fk() + "]";
			outTrx.setRtn_mesg(gszMsgBuf);
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			return ;
		}*/

		//将需要上报spc的数据重新整理
		//inTrx = checkUploadIary(inTrx,flag);

		XpspcchkO xpspcchkO;
		try {
			xpspcchkO = reportSPC(inTrx);
			
			//if inTrx.check_spc_flg  does not equal("Y") , ignore the outTrx of xpspcchkService;
			if (!xpspcchkO.getRtn_code().equals(RETURN_CODE_OK)) {
				gszMsgBuf = xpspcchkO.getRtn_mesg();
				outTrx.setRtn_code(xpspcchkO.getRtn_code());
				outTrx.setRtn_mesg(gszMsgBuf);
				return ;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		return;
	}

	//重新组合自动上报的数据
	public XpcmsrdtI initIary(XpcmsrdtI inTrx,String flag){
		if("A".equals(flag)){
			XpcmsrdtIA2 xpcmsrdtIA2= inTrx.getIary2();
			List<XpcmsrdtIA> iaryList = new ArrayList<XpcmsrdtIA>();

			List<List<String>> dataValueList= xpcmsrdtIA2.getData_value();
			for(int i = 0; i < dataValueList.size(); i++){
				List<String> dataValueL =dataValueList.get(i);
				for(int j = 0 ;j < dataValueL.size(); j++){
					String dataValue = dataValueL.get(j);
					XpcmsrdtIA iary = new XpcmsrdtIA();
					iary.setData_value(dataValue);
					iaryList.add(iary);
				}
			}
			inTrx.setIary(iaryList);
			inTrx.setData_cnt(String.valueOf(iaryList.size()));
		}
		return inTrx;
	}


	private XpspcchkO reportSPC(XpcmsrdtI inTrx) throws Exception {
		XpspcchkO xpspcchkO = new XpspcchkO();
		xpspcchkO.setTrx_id("XPSPCCHK");
		xpspcchkO.setTrx_id("O");

		XpspcchkI xpspcchkI = buildSpcInTrx(inTrx);

		String strOutTrx = xpspcchkService.subMainProc(AppContext.getCurrEventNumber(),
				JacksonUtil.toJSONStr(xpspcchkI));

		xpspcchkO = JacksonUtil.fromJson(strOutTrx, XpspcchkO.class);
		return xpspcchkO;
	}

	public XpcmsrdtI checkUploadIary(XpcmsrdtI inTrx,String flag){
		String toolId = inTrx.getTool_id();
		String mesIdFk = inTrx.getMes_id_fk();
		//String repUnitFk = inTrx.getRep_unit_fk();//"P"
		String repUnitFk = "P";
//		String dataPatFk = inTrx.getData_pat_fk();
		String dataPatFk = "00";

		//查询bis_m_item
		String hql = "FROM Bis_m_item where 1=1";
		if (toolId !=null && toolId.length() != 0) {
			hql = hql + " and tool_id_fk='" + toolId + "'";
		}
		if (repUnitFk != null && repUnitFk.length() != 0) {
			hql = hql + " and rep_unit_fk ='" + repUnitFk + "'";
		}
		if (dataPatFk != null && dataPatFk.length() != 0) {
			hql = hql + " and data_pat_fk='" + dataPatFk + "'";
		}
		if (mesIdFk != null && mesIdFk.length() != 0) {
			hql = hql + " and mes_id_fk='" + mesIdFk + "'";
		}
		/*hql = hql + " and opi_data_flg ='Y'";*/
		List<Bis_m_item> bis_m_items= bisMItemDao.find(hql);

		List<XpcmsrdtIA> iaryList = inTrx.getIary();
		List<XpcmsrdtIA> newIaryList = new ArrayList<XpcmsrdtIA>();

		if(!"A".equals(flag)){
			iaryList = inTrx.getIary();
			for(int i = 0 ; i < iaryList.size(); i ++){
				XpcmsrdtIA iary = iaryList.get(i);
				String dataGroup  = iary.getData_group();//参数组名 cd
				String dataValue = iary.getData_value();
				String dataGroupSeq = iary.getData_group_seq();//组内顺序
				for(int j = 0 ; j < bis_m_items.size() ; j++){
					if(bis_m_items.get(j).getData_group().equals(dataGroup) &&
							bis_m_items.get(j).getData_group_seq().equals(dataGroupSeq)){
						XpcmsrdtIA newIary = new XpcmsrdtIA();
						newIary.setData_group(dataGroup);
						newIary.setData_value(dataValue);
						newIary.setData_group_seq(dataGroupSeq);
						newIaryList.add(newIary);
					}else{
						continue;
					}
				}
			}
			inTrx.setIary(newIaryList);
			inTrx.setData_cnt(String.valueOf(newIaryList.size()));
			return  inTrx;
		}else{//自动上报
			//List<XpcmsrdtIA2> iary2List= inTrx.getIary2();
			XpcmsrdtIA2 iary2 = inTrx.getIary2();
			List<String> dataGroupList= iary2.getData_group();
			List<List<String>> dataValueList= iary2.getData_value();
			//重组数据后  上报顺序按行来上报
			List<XpcmsrdtIA> iaryList2 = new ArrayList<XpcmsrdtIA>();
			for(int j = 0; j < dataValueList.size(); j++){
				List<String> dataValueL = dataValueList.get(j);
				for(int m = 0; m < dataValueL.size() ; m++){
					XpcmsrdtIA newIary = new XpcmsrdtIA();
					newIary.setData_group(dataGroupList.get(m));
					newIary.setData_value(dataValueL.get(m));
					iaryList2.add(newIary);
				}
			}

			//处理上报的数据
			for(int i = 0 ;i < bis_m_items.size();i++){
				String dataId = bis_m_items.get(i).getId().getData_id();
				String dataGroupSeq = bis_m_items.get(i).getData_group_seq();
				Integer dataID = Integer.parseInt(dataId);
				XpcmsrdtIA newIary = new XpcmsrdtIA();
				String dataGroup = iaryList2.get(dataID).getData_group();//必须有
				String dataValue = iaryList2.get(dataID).getData_value();//必须有
				newIary.setData_group(dataGroup);
				newIary.setData_value(dataValue);
				newIary.setData_group_seq(dataGroupSeq);
				newIaryList.add(newIary);
			}
			inTrx.setIary(newIaryList);
			inTrx.setData_cnt(String.valueOf(newIaryList.size()));
			return  inTrx;
		}
	}

	private XpspcchkI buildSpcInTrx(XpcmsrdtI inTrx) {
		String mesIdFk = inTrx.getMes_id_fk();

		XpspcchkI xpspcchkI = null;
		xpspcchkI = buildCommonSpcInTrx(inTrx);
		return xpspcchkI;
	}

	private XpspcchkI buildCdpcSpcInTrx(Ret_prd_info retPrdInfo, Ret_wo retWo, XpcmsrdtI inTrx) {

		//when the sheet is on unpack process ,get the from_thickness of it's work order,otherwise get the to_thickness;

		XpspcchkI xpspcchkI = buildCommonSpcInTrx(inTrx);

		xpspcchkI.setCdpc_flg("Y");
		xpspcchkI.setTarget_thickness(inTrx.getTarget_thickness());
		xpspcchkI.setThickness_type(inTrx.getThickness_type());

		return xpspcchkI;
	}

	/**
	 * special CDPC process
	 * condition : if the inTrx was sent from page 1100 and 1400 and it's data_group is CDPC,
	 *             the cdpc_flg of inTrx will be Y
	 * action:  get the thickness of the sheet's work order then send to SPC.
	 *          the service XpspcchkService will find which chart's data_group is CDPC 
	 *          and it's target line equals the thickness of work oder,
	 *          pay attention!!!  XpspcchkService will ignore all the sub group condition,
	 *          so XpspcchkService may be heavily modified than expected;  
	 * @param inTrx 
	 *          
	 */
	private XpspcchkI buildCommonSpcInTrx(XpcmsrdtI inTrx) {
		Timestamp gsTimestamp = Util.getCurrentTimestamp();
		XpspcchkI xpspcchkI = new XpspcchkI();
		xpspcchkI.setTrx_id("XPSPCCHK");
		xpspcchkI.setType_id("O");

		xpspcchkI.setData_cnt(inTrx.getData_cnt());
		xpspcchkI.setClm_user(inTrx.getRpt_usr());
		//xpspcchkI.setProd_seq_id(retPrdInfo.getPrd_seq_id());
		xpspcchkI.setCol_typ("2");
		/*xpspcchkI.setMdl_typ(retPrdInfo.getMdl_typ_fk());
		xpspcchkI.setMdl_cate(retPrdInfo.getMdl_cate_fk());*/
		//xpspcchkI.setMdl_id(retPrdInfo.getMdl_id_fk());
		xpspcchkI.setP_tool_id(inTrx.getTool_id());
		xpspcchkI.setP_path_id(inTrx.getPath_id());
		xpspcchkI.setP_path_ver(inTrx.getPath_ver());
		xpspcchkI.setP_ope_no(inTrx.getOpe_no());
		xpspcchkI.setP_ope_id(inTrx.getOpe_id());
		xpspcchkI.setP_ope_ver(inTrx.getOpe_ver());
		xpspcchkI.setP_proc_id(inTrx.getProc_id());
		xpspcchkI.setM_tool_id(inTrx.getTool_id());
		xpspcchkI.setM_path_id(inTrx.getPath_id());
		xpspcchkI.setM_path_ver(inTrx.getPath_ver());
		xpspcchkI.setM_ope_no(inTrx.getOpe_no());
		xpspcchkI.setM_ope_id(inTrx.getOpe_id());
		xpspcchkI.setM_ope_ver(inTrx.getOpe_ver());
		xpspcchkI.setM_proc_id(inTrx.getProc_id());
		xpspcchkI.setEvt_usr(inTrx.getRpt_usr());
		xpspcchkI.setEvt_timestamp(inTrx.getEvt_timestamp());
		/*if(StringUtils.isEmpty(inTrx.getLot_id())){
			xpspcchkI.setLot_id_fk(retPrdInfo.getLot_id());
		}else{
			xpspcchkI.setLot_id_fk(inTrx.getLot_id());
		}*/
		
	/*	xpspcchkI.setWo_id_fk(retPrdInfo.getWo_id_fk());
		xpspcchkI.setCus_id_fk(retWo.getCus_id_fk());
		xpspcchkI.setSo_id_fk(retPrdInfo.getSo_id_fk());
*/
		List<XpcmsrdtIA> edcIaryList = inTrx.getIary();
		List<XpspcchkIA> iaryList = new ArrayList<XpspcchkIA>();
		for (XpcmsrdtIA edcIary : edcIaryList) {
			XpspcchkIA iary = new XpspcchkIA();
			iary.setData_group(edcIary.getData_group());
			iary.setData_value(edcIary.getData_value());
			iary.setData_group_seq(edcIary.getData_group_seq());
			iaryList.add(iary);
		}

		xpspcchkI.setIary(iaryList);
		xpspcchkI.setData_cnt(inTrx.getData_cnt());
		return xpspcchkI;
	}

	private String setPrdAdminFlgWhenCdpcCzpc(Ret_prd_info retPrdInfo, XpcmsrdtI inTrx) {
		StringBuffer buffer = new StringBuffer(retPrdInfo.getPrd_admin_flg());

		if (("CDPC").equals(inTrx.getMes_id_fk())) {
			if (("NG").equals(inTrx.getJudgeStr())) {
				buffer.setCharAt(2, 'Y');
			} else {
				buffer.setCharAt(2, 'N');
			}
		} else if (("CZPC").equals(inTrx.getMes_id_fk())) {
			if (("NG").equals(inTrx.getJudgeStr())) {
				buffer.setCharAt(3, 'Y');
			} else {
				buffer.setCharAt(3, 'N');
			}

		}

		retPrdInfo.setPrd_admin_flg(buffer.toString());
		retPrdInfoDao.update(retPrdInfo);
		return RETURN_CODE_OK;
	}

	public SimpleRtnBeen setEDCDataValue(Ret_prd_info ret_prd_info, XpcmsrdtI inTrx) {
		String gszMsgBuf ="";
		Timestamp gsTimestamp = Util.getCurrentTimestamp();
		String gsEvtSequenceID =GUIDGenerator.javaGUID();
		int data_cnt = Integer.valueOf(inTrx.getData_cnt());
		List<XpcmsrdtIA> iaryList = inTrx.getIary();
		int data_seq_cnt = data_cnt / 30 + 1;
		int data_index = 0;
		int MAX_DATA_VALUE_CNT = 30;

		String tool_id = inTrx.getTool_id();
		String mes_id= "";
		Bis_pamId pam_id = new Bis_pamId();
		pam_id.setTool_id_fk(tool_id);
		pam_id.setMdl_id_fk(ret_prd_info.getMdl_id_fk());
		pam_id.setOpe_id_fk(ret_prd_info.getCr_ope_id_fk());
		pam_id.setOpe_no_fk(ret_prd_info.getCr_ope_no_fk());
		pam_id.setOpe_ver_fk(ret_prd_info.getCr_ope_ver_fk());
		pam_id.setPath_id_fk(ret_prd_info.getCr_path_id_fk());
		pam_id.setPath_ver_fk(ret_prd_info.getCr_path_ver_fk());
		Bis_pam bis_pam = (Bis_pam)this.bisPamDao.get(pam_id);
		if(bis_pam == null) {
			String hql = "FROM Bis_pam where tool_id_fk='" + tool_id + "' and mdl_id_fk='" + ret_prd_info.getMdl_id_fk() + "' and ope_id_fk='" +inTrx.getOpe_id() + "' and ope_ver_fk='" + inTrx.getOpe_ver() + "'";
			List<Bis_pam> bis_pams = this.bisPamDao.find(hql);
			if(bis_pams.isEmpty()) {
				String hql_ope = "FROM Bis_pam where tool_id_fk='" + tool_id + "' and ope_id_fk='" + inTrx.getOpe_id() + "' and ope_ver_fk='" + inTrx.getOpe_ver() + "'";
				List<Bis_pam> bis_pams_ope = this.bisPamDao.find(hql_ope);
				if(bis_pams_ope.isEmpty()) {
					gszMsgBuf = "该设备[" + tool_id + "]在该站点[" + ret_prd_info.getCr_ope_id_fk() + "]没有维护parameter信息";
					return new SimpleRtnBeen(3003601L, gszMsgBuf);
				}
				mes_id = ((Bis_pam)bis_pams_ope.get(0)).getMes_id();
			} else {
				mes_id = ((Bis_pam)bis_pams.get(0)).getMes_id();
			}
		} else {
			mes_id = bis_pam.getMes_id();
		}

		String mes_id_fk = inTrx.getMes_id_fk();//量测属性
		/*if(!mes_id.equals(mes_id_fk)){
			gszMsgBuf = "请确认在parameter信息中维护的量测名称与选择上报的机台参数是否一致！";
			return new SimpleRtnBeen(3003611L, gszMsgBuf);
		}*/
		String prd_seq_id = inTrx.getPrd_seq_id();
		String tool_id_fk = inTrx.getTool_id();
		String rep_unit_fk = inTrx.getRep_unit_fk();//上报单元  P（如果是手动单片则为P ，自动为S，批量为T？）
		String data_pat_fk = inTrx.getData_pat_fk();//上报属性 默认EDC 00
		String box_id_fk = ret_prd_info.getBox_id_fk();
		String slot_no_fk = ret_prd_info.getSlot_no();
		String path_id_fk = ret_prd_info.getCr_path_id_fk();
		String path_ver_fk = ret_prd_info.getCr_path_ver_fk();
		String ope_no_fk = ret_prd_info.getCr_ope_no_fk();
		String ope_id_fk = ret_prd_info.getCr_ope_id_fk();
		String ope_ver_fk = ret_prd_info.getCr_ope_ver_fk();
		String ds_recipe_id = "TODO";
		String ac_recipe_id = "TODO";
		String rpt_usr = inTrx.getRpt_usr();

		for (int i = 0; i < data_seq_cnt; i++) {
			Spc_ret_mfg_data spc_ret_mfg_data = new Spc_ret_mfg_data();
			Spc_ret_mfg_dataId id = new Spc_ret_mfg_dataId();
			id.setData_seq_id_fk(i + 1);
			id.setEvt_seq_id_fk(gsEvtSequenceID);
			id.setPrd_seq_id_fk(prd_seq_id);
			spc_ret_mfg_data.setId(id);
			spc_ret_mfg_data.setUnq_seq_id(GUIDGenerator.javaGUID());
			spc_ret_mfg_data.setTool_id_fk(tool_id_fk);
			spc_ret_mfg_data.setRep_unit_fk("P");//默认sub sheet
			spc_ret_mfg_data.setData_pat_fk("00");//默认EDC
			spc_ret_mfg_data.setMes_id_fk(mes_id_fk);
			spc_ret_mfg_data.setBox_id_fk(box_id_fk);
			spc_ret_mfg_data.setSlot_no_fk(slot_no_fk);
			spc_ret_mfg_data.setPath_id_fk(path_id_fk);
			spc_ret_mfg_data.setPath_ver_fk(path_ver_fk);
			spc_ret_mfg_data.setOpe_id_fk(ope_id_fk);
			spc_ret_mfg_data.setOpe_no_fk(ope_no_fk);
			spc_ret_mfg_data.setOpe_ver_fk(ope_ver_fk);
			spc_ret_mfg_data.setDs_recipe_id(ds_recipe_id);
			spc_ret_mfg_data.setAc_recipe_id(ac_recipe_id);
			spc_ret_mfg_data.setRpt_usr(rpt_usr);
			spc_ret_mfg_data.setRpt_timestamp(gsTimestamp);
			spc_ret_mfg_data.setData_cnt(data_cnt);
			spc_ret_mfg_data.setWo_id_fk(ret_prd_info.getWo_id_fk());

			for (int j = 0; j < MAX_DATA_VALUE_CNT; j++) {
				data_index = i * MAX_DATA_VALUE_CNT + j;
				if (data_index == data_cnt) {
					break;
				}
				String data_value = iaryList.get(data_index).getData_value();
				if (j == 0) {
					spc_ret_mfg_data.setData_value_001(data_value);
				} else if (j == 1) {
					spc_ret_mfg_data.setData_value_002(data_value);
				} else if (j == 2) {
					spc_ret_mfg_data.setData_value_003(data_value);
				} else if (j == 3) {
					spc_ret_mfg_data.setData_value_004(data_value);
				} else if (j == 4) {
					spc_ret_mfg_data.setData_value_005(data_value);
				} else if (j == 5) {
					spc_ret_mfg_data.setData_value_006(data_value);
				} else if (j == 6) {
					spc_ret_mfg_data.setData_value_007(data_value);
				} else if (j == 7) {
					spc_ret_mfg_data.setData_value_008(data_value);
				} else if (j == 8) {
					spc_ret_mfg_data.setData_value_009(data_value);
				} else if (j == 9) {
					spc_ret_mfg_data.setData_value_010(data_value);
				} else if (j == 10) {
					spc_ret_mfg_data.setData_value_011(data_value);
				} else if (j == 11) {
					spc_ret_mfg_data.setData_value_012(data_value);
				} else if (j == 12) {
					spc_ret_mfg_data.setData_value_013(data_value);
				} else if (j == 13) {
					spc_ret_mfg_data.setData_value_014(data_value);
				} else if (j == 14) {
					spc_ret_mfg_data.setData_value_015(data_value);
				} else if (j == 15) {
					spc_ret_mfg_data.setData_value_016(data_value);
				} else if (j == 16) {
					spc_ret_mfg_data.setData_value_017(data_value);
				} else if (j == 17) {
					spc_ret_mfg_data.setData_value_018(data_value);
				} else if (j == 18) {
					spc_ret_mfg_data.setData_value_019(data_value);
				} else if (j == 19) {
					spc_ret_mfg_data.setData_value_020(data_value);
				} else if (j == 20) {
					spc_ret_mfg_data.setData_value_021(data_value);
				} else if (j == 21) {
					spc_ret_mfg_data.setData_value_022(data_value);
				} else if (j == 22) {
					spc_ret_mfg_data.setData_value_023(data_value);
				} else if (j == 23) {
					spc_ret_mfg_data.setData_value_024(data_value);
				} else if (j == 24) {
					spc_ret_mfg_data.setData_value_025(data_value);
				} else if (j == 25) {
					spc_ret_mfg_data.setData_value_026(data_value);
				} else if (j == 26) {
					spc_ret_mfg_data.setData_value_027(data_value);
				} else if (j == 27) {
					spc_ret_mfg_data.setData_value_028(data_value);
				} else if (j == 28) {
					spc_ret_mfg_data.setData_value_029(data_value);
				} else if (j == 29) {
					spc_ret_mfg_data.setData_value_030(data_value);
				}
			}
			spcRetMfgDataDao.save(spc_ret_mfg_data);
		}
		return new SimpleRtnBeen(_NORMAL);
	}

	public String reportEqptSPCDatqa(XpcmsrdtI inTrx) {
		Timestamp gsTimestamp = null;
		String gsEvtSequenceID ="";
		int data_cnt = Integer.valueOf(inTrx.getData_cnt());
		List<XpcmsrdtIA> iaryList = inTrx.getIary();
		int data_seq_cnt = data_cnt / 30 + 1;
		int data_index = 0;
		int MAX_DATA_VALUE_CNT = 30;
		int id_cnt = 0;
		if (inTrx.getId_cnt() == null) {
			id_cnt = 0;
		}else{
			id_cnt = inTrx.getId_cnt().intValue();
		}
		if ( id_cnt == 0 ){
			for (int i = 0; i < data_seq_cnt; i++) {
				Spc_ret_mfg_data spc_ret_mfg_data = new Spc_ret_mfg_data();
				Spc_ret_mfg_dataId id = new Spc_ret_mfg_dataId();
				id.setData_seq_id_fk(i + 1);
				id.setEvt_seq_id_fk(gsEvtSequenceID);
				id.setPrd_seq_id_fk(inTrx.getPrd_seq_id());
				
				spc_ret_mfg_data.setId(id);
				spc_ret_mfg_data.setUnq_seq_id(GUIDGenerator.javaGUID());
				spc_ret_mfg_data.setTool_id_fk(inTrx.getTool_id());
				spc_ret_mfg_data.setRep_unit_fk(inTrx.getRep_unit_fk());
				spc_ret_mfg_data.setData_pat_fk(inTrx.getData_pat_fk());
				spc_ret_mfg_data.setMes_id_fk(inTrx.getMes_id_fk());
				spc_ret_mfg_data.setData_cnt(data_cnt);
				spc_ret_mfg_data.setRpt_usr(inTrx.getRpt_usr());
				spc_ret_mfg_data.setRpt_timestamp(gsTimestamp);

				for (int j = 0; j < MAX_DATA_VALUE_CNT; j++) {
					data_index = i * MAX_DATA_VALUE_CNT + j;
					if (data_index == data_cnt) {
						break;
					}
					String data_value = iaryList.get(data_index).getData_value();
					if (j == 0) {
						spc_ret_mfg_data.setData_value_001(data_value);
					} else if (j == 1) {
						spc_ret_mfg_data.setData_value_002(data_value);
					} else if (j == 2) {
						spc_ret_mfg_data.setData_value_003(data_value);
					} else if (j == 3) {
						spc_ret_mfg_data.setData_value_004(data_value);
					} else if (j == 4) {
						spc_ret_mfg_data.setData_value_005(data_value);
					} else if (j == 5) {
						spc_ret_mfg_data.setData_value_006(data_value);
					} else if (j == 6) {
						spc_ret_mfg_data.setData_value_007(data_value);
					} else if (j == 7) {
						spc_ret_mfg_data.setData_value_008(data_value);
					} else if (j == 8) {
						spc_ret_mfg_data.setData_value_009(data_value);
					} else if (j == 9) {
						spc_ret_mfg_data.setData_value_010(data_value);
					} else if (j == 10) {
						spc_ret_mfg_data.setData_value_011(data_value);
					} else if (j == 11) {
						spc_ret_mfg_data.setData_value_012(data_value);
					} else if (j == 12) {
						spc_ret_mfg_data.setData_value_013(data_value);
					} else if (j == 13) {
						spc_ret_mfg_data.setData_value_014(data_value);
					} else if (j == 14) {
						spc_ret_mfg_data.setData_value_015(data_value);
					} else if (j == 15) {
						spc_ret_mfg_data.setData_value_016(data_value);
					} else if (j == 16) {
						spc_ret_mfg_data.setData_value_017(data_value);
					} else if (j == 17) {
						spc_ret_mfg_data.setData_value_018(data_value);
					} else if (j == 18) {
						spc_ret_mfg_data.setData_value_019(data_value);
					} else if (j == 19) {
						spc_ret_mfg_data.setData_value_020(data_value);
					} else if (j == 20) {
						spc_ret_mfg_data.setData_value_021(data_value);
					} else if (j == 21) {
						spc_ret_mfg_data.setData_value_022(data_value);
					} else if (j == 22) {
						spc_ret_mfg_data.setData_value_023(data_value);
					} else if (j == 23) {
						spc_ret_mfg_data.setData_value_024(data_value);
					} else if (j == 24) {
						spc_ret_mfg_data.setData_value_025(data_value);
					} else if (j == 25) {
						spc_ret_mfg_data.setData_value_026(data_value);
					} else if (j == 26) {
						spc_ret_mfg_data.setData_value_027(data_value);
					} else if (j == 27) {
						spc_ret_mfg_data.setData_value_028(data_value);
					} else if (j == 28) {
						spc_ret_mfg_data.setData_value_029(data_value);
					} else if (j == 29) {
						spc_ret_mfg_data.setData_value_030(data_value);
					}
				}
				spcRetMfgDataDao.save(spc_ret_mfg_data);
			}
		}else {
			for (int k = 0; k < id_cnt ; k ++){
				for (int i = 0; i < data_seq_cnt; i++) {
					Spc_ret_mfg_data spc_ret_mfg_data = new Spc_ret_mfg_data();
					Spc_ret_mfg_dataId id = new Spc_ret_mfg_dataId();
					id.setData_seq_id_fk(i + 1);
					id.setEvt_seq_id_fk(gsEvtSequenceID);
					id.setPrd_seq_id_fk(inTrx.getIary_id().get(k).getPrd_seq_id_fk());
					
					spc_ret_mfg_data.setId(id);
					spc_ret_mfg_data.setUnq_seq_id(GUIDGenerator.javaGUID());
					spc_ret_mfg_data.setTool_id_fk(inTrx.getTool_id());
					spc_ret_mfg_data.setRep_unit_fk(inTrx.getRep_unit_fk());
					spc_ret_mfg_data.setData_pat_fk(inTrx.getData_pat_fk());
					spc_ret_mfg_data.setMes_id_fk(inTrx.getMes_id_fk());
					spc_ret_mfg_data.setData_cnt(data_cnt);
					spc_ret_mfg_data.setRpt_usr(inTrx.getRpt_usr());
					spc_ret_mfg_data.setRpt_timestamp(gsTimestamp);

					for (int j = 0; j < MAX_DATA_VALUE_CNT; j++) {
						data_index = i * MAX_DATA_VALUE_CNT + j;
						if (data_index == data_cnt) {
							break;
						}
						String data_value = iaryList.get(data_index).getData_value();
						if (j == 0) {
							spc_ret_mfg_data.setData_value_001(data_value);
						} else if (j == 1) {
							spc_ret_mfg_data.setData_value_002(data_value);
						} else if (j == 2) {
							spc_ret_mfg_data.setData_value_003(data_value);
						} else if (j == 3) {
							spc_ret_mfg_data.setData_value_004(data_value);
						} else if (j == 4) {
							spc_ret_mfg_data.setData_value_005(data_value);
						} else if (j == 5) {
							spc_ret_mfg_data.setData_value_006(data_value);
						} else if (j == 6) {
							spc_ret_mfg_data.setData_value_007(data_value);
						} else if (j == 7) {
							spc_ret_mfg_data.setData_value_008(data_value);
						} else if (j == 8) {
							spc_ret_mfg_data.setData_value_009(data_value);
						} else if (j == 9) {
							spc_ret_mfg_data.setData_value_010(data_value);
						} else if (j == 10) {
							spc_ret_mfg_data.setData_value_011(data_value);
						} else if (j == 11) {
							spc_ret_mfg_data.setData_value_012(data_value);
						} else if (j == 12) {
							spc_ret_mfg_data.setData_value_013(data_value);
						} else if (j == 13) {
							spc_ret_mfg_data.setData_value_014(data_value);
						} else if (j == 14) {
							spc_ret_mfg_data.setData_value_015(data_value);
						} else if (j == 15) {
							spc_ret_mfg_data.setData_value_016(data_value);
						} else if (j == 16) {
							spc_ret_mfg_data.setData_value_017(data_value);
						} else if (j == 17) {
							spc_ret_mfg_data.setData_value_018(data_value);
						} else if (j == 18) {
							spc_ret_mfg_data.setData_value_019(data_value);
						} else if (j == 19) {
							spc_ret_mfg_data.setData_value_020(data_value);
						} else if (j == 20) {
							spc_ret_mfg_data.setData_value_021(data_value);
						} else if (j == 21) {
							spc_ret_mfg_data.setData_value_022(data_value);
						} else if (j == 22) {
							spc_ret_mfg_data.setData_value_023(data_value);
						} else if (j == 23) {
							spc_ret_mfg_data.setData_value_024(data_value);
						} else if (j == 24) {
							spc_ret_mfg_data.setData_value_025(data_value);
						} else if (j == 25) {
							spc_ret_mfg_data.setData_value_026(data_value);
						} else if (j == 26) {
							spc_ret_mfg_data.setData_value_027(data_value);
						} else if (j == 27) {
							spc_ret_mfg_data.setData_value_028(data_value);
						} else if (j == 28) {
							spc_ret_mfg_data.setData_value_029(data_value);
						} else if (j == 29) {
							spc_ret_mfg_data.setData_value_030(data_value);
						}
					}
					spcRetMfgDataDao.save(spc_ret_mfg_data);
				}
			}
		}
		return RETURN_CODE_OK;
	}
}
